package com.woniuxy.woniuticket.ordermodule.service.serviceimpl;

import com.woniuxy.woniuticket.movieinfomodule.dao.CinemaInfoDAO;
import com.woniuxy.woniuticket.ordermodule.dto.OrderDTO;
import com.woniuxy.woniuticket.ordermodule.service.OrderService;
import com.woniuxy.woniuticket.pojo.CinemaInfo;
import com.woniuxy.woniuticket.pojo.CinemaSeats;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Created by Wu xianbo on 2019/6/10
 */

@Service
public class OrderCache {

    @Autowired
    private OrderService orderService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;    //redis模板
    @Autowired
    private CinemaInfoDAO cinemaInfoDAO;  //影院信息dao，用于查询影院logo

    //redis中缓存未支付订单。页面上查询所有未支付订单均从缓冲中取
    public void orderCacheInsert(String orderNumber,Integer userinfoId,List<CinemaSeats> movieSeats){
        System.out.println("缓存中插入未支付订单");
        //先查询订单详情
        OrderDTO orderDTO = orderService.findOrderByOrderNumber(orderNumber);
        //往订单中插入座位信息
        orderDTO.setSeatList(movieSeats);
        //往redis缓存中存入订单详情
        String key = "user"+userinfoId+":"+orderNumber;
        //设置序列器
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(OrderDTO.class));
        //存储数据
        redisTemplate.opsForValue().set(key,orderDTO);
        //设置过期时间
        redisTemplate.expire(key, 30, TimeUnit.MINUTES);
    }

    //订单支付成功后删除缓存中的未支付订单
    public void orderCacheDelete(String orderNumber,Integer userinfoId){
        System.out.println("缓存中删除未支付订单");
        String key = "user"+userinfoId+":"+orderNumber;
        redisTemplate.delete(key);
    }

    //查询缓存中该用户的所有未支付订单
    public List<OrderDTO> selectOrderBykey(Integer userinfoId){
        System.out.println("查询缓存中该支付订单");
        //设置序列器
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(OrderDTO.class));
        //模糊查询数据
        Set<String> keys = redisTemplate.keys("user"+userinfoId+ "*");
        System.out.println("打印所有的key："+keys);
        List<OrderDTO> orderDTOS = new ArrayList<>();
        for (String key : keys) {
            //将内容查询出来放入list并返回
            OrderDTO orderDTO = (OrderDTO) redisTemplate.opsForValue().get(key);
            if (orderDTO!= null) {
                Long expire = redisTemplate.getExpire(key, TimeUnit.MINUTES);   //订单过期时间
                System.out.println("过期时间为："+expire);
                orderDTO.setTimeOut(expire);           //封装过期时间
                orderDTOS.add(orderDTO);
            }
        }
        return orderDTOS;
    }
    
    //根据订单号查询该用户的未支付订单(提交选座后用)
    public OrderDTO getOrderInfoByOrdernum(String orderNum,Integer userInfo){
        System.out.println("缓存中根据订单号查询未支付订单");
        //设置序列器
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(OrderDTO.class));
        String key =  "user"+userInfo+":"+orderNum;
        OrderDTO orderDTO = (OrderDTO) redisTemplate.opsForValue().get(key);
        return orderDTO;
    }
}
